// formily 内置appHelper常用操作
import _get from 'lodash/get';
import { Schema } from "@formily/antd";
// import _set from 'lodash/set'

export function isIdDuplicated(appHelper, currentId) {
  if (appHelper && appHelper.activeKey) {
    const { activeKey } = appHelper;
    const schemas = _get(appHelper, 'schemaHelper.schemas', []).slice(0);
    let id;
    let _id;
    let schema;
    while(schemas.length > 0) {
      schema = schemas.shift();
      id = _get(schema, 'props.id', null);
      _id = _get(schema, 'props._id', null);
      if (id !== activeKey && _id === currentId) {
        return true;
      }
      if (schema.children && schema.children.length > 0) {
        schemas.push(...schema.children);
      }
    }
  }
}

export const getFormTitleKeyList = (paramSchema) => {
  const schema = new Schema(paramSchema);
  const results = [];
  const mapper = (_schema, key) => {
    const { 'x-component': xComp, title } = _schema;
    if (xComp !== 'card' && xComp !== 'grid') {
      results.push({
        title,
        key,
      });
    } else {
       new Schema(_schema).mapProperties(mapper);
    }
  };

  schema.mapProperties(mapper);

  return results;
}